
from selenium import  webdriver
from selenium.webdriver.support.select import Select


class BasePage:

    def __init__(self):
        self.driver=webdriver.Chrome()

    def get_element(self,locater):
        element=self.driver.find_element(*locater)
        return element

    def input(self,locater,text):
        element=self.get_element(locater)
        element.send_keys(text)

    def click(self,locater):
        element=self.get_element(locater)
        element.click()

    def clear(self,locater):
        element=self.get_element(locater)
        element.clear()

    def switch_window(self,index=-1):
        handles=self.driver.window_handles
        self.driver.switch_to.window(handles[index])

    def switch_into_frame(self,locater):
        element=self.get_element(locater)
        self.driver.switch_to(element)

    def switch_outo_frame(self):
        self.driver.switch_to.default_content()


    def switch_alter(self,action='a',text=None):
        alter=self.driver.switch_to.alert
        if action=='a':
            alter.accept()
        elif action=='d':
            alter.dismiss()
        elif action=='t':
            return alter.text
        else:
            alter.send_keys(text)
            alter.accept()


    def select(self,locater,index=None,value=None,visible_text=None):
        elemnet=self.get_element(locater)
        select=Select(elemnet)
        if index is not None and value is None and visible_text is None:
            select.select_by_index(index)
        if index is  None and value is not None and visible_text is None:
            select.select_by_value(value)
        if index is  None and value is None and visible_text is not None:
            select.select_by_visible_text(visible_text)


    def execute_js(self,js,element=None):
        self.driver.execute_script(js,element)
